package gui;

import interfaces.IObservadorControl;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

import constantes.ICTE;
import controlador.Controlador;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class MainGui extends javax.swing.JFrame implements IObservadorControl {
	private JButton BTServer;
	private JButton BTCliente;
	private JPopupMenu jPopupMenu1;
	private PantallaServer server;
	private PantallaCliente cliente;
	private Controlador controlador;
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Controlador controlador = new Controlador();
				MainGui inst = new MainGui(controlador);
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public MainGui(Controlador controlador) {
		super();
		this.controlador = controlador;
		this.controlador.addObserver(this);
		initGUI();
	}
	
	private void initGUI() {
		try {
			{
				this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
				this.setDefaultLookAndFeelDecorated(true);
				getContentPane().setLayout(null);
				this.setTitle("Tuti-Fruti");
				{
					BTServer = new JButton();
					getContentPane().add(BTServer);
					BTServer.setText("Server");
					BTServer.setBounds(227, 98, 88, 22);
					BTServer.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent e) {
							System.out.println("Clickeo en Server..");
							controlador.iniciar(ICTE.MODO_SERVER);
							//mostrarPantalla(); 
						}
					});
				}
				{
					BTCliente = new JButton();
					getContentPane().add(BTCliente);
					BTCliente.setText("Cliente");
					BTCliente.setBounds(227, 135, 88, 22);
					BTCliente.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent e) {
							System.out.println("Clickeo en CLiente..");
							controlador.iniciar(ICTE.MODO_CLIENTE);
							//mostrarPantalla(ICTE.MODO_CLIENTE); 
						}
					});
				}

			}
			setSize(400, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void mostrarPantalla(short modo){
				
		this.setVisible(false);
		
		if (modo == ICTE.MODO_SERVER){
			
			server = new PantallaServer(this);
			server.setVisible(true);
		}
		else {
			cliente = new PantallaCliente(this);
			cliente.setVisible(true);			
		}
	}

	public void cargarPantallaCliente() {
		mostrarPantalla(ICTE.MODO_CLIENTE);		
	}

	public void cargarPantallaServer() {
		mostrarPantalla(ICTE.MODO_SERVER);		
	}
	

}
